package com.bytedance.frameworks.baselib.network.http.b.a;

import java.io.IOException;
import java.net.CookieHandler;
import java.net.URI;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.platform.Platform;

public final class JavaNetCookieJar implements Interceptor {
    public JavaNetCookieJar() {

    }

    public final Response intercept(RealInterceptorChain arg13) throws IOException {
        HashMap v3_2;
        Request v1_4 = null;
        StringBuilder v10;
        Map<String, List<String>> v1_3 = null;
        Object v1_2;
        Iterator v5;
        Set v1_1;
        HashMap v2;
        Request v4 = arg13.request();
        URI v6 = v4.url().uri();
        CookieHandler v7 = CookieHandler.getDefault();
        if (v7 != null) {
            try {
                v2 = new HashMap();
                Headers headers = v4.headers();
                if (headers != null) {
                    v1_1 = headers.names();
                    if (v1_1 != null) {
                        v5 = v1_1.iterator();
                        while (v5.hasNext()) {
                            v1_2 = v5.next();
                            ((Map) v2).put(v1_2, headers.values(((String) v1_2)));
                        }
                    }
                }
                v1_3 = v7.get(v6, v2);
            } catch (IOException v2_1) {
                Platform.get().log(5, "Loading cookies failed for " + v6.resolve("/..."), v2_1);

            }

            if (v1_3 != null) {
                if (v1_3.size() > 0) {
                    Request.Builder v8 = v4.newBuilder();
                    Iterator<Entry<String, List<String>>> v9 = v1_3.entrySet().iterator();
                    do {
                        label_46:
                        if (v9.hasNext()) {
                            Entry<String, List<String>> v2_2 = v9.next();
                            String v3_1 = v2_2.getKey();
                            v10 = new StringBuilder();
                            if (!"X-SS-Cookie".equalsIgnoreCase(v3_1) && !"Cookie".equalsIgnoreCase(v3_1) && !"Cookie2".equalsIgnoreCase(v3_1)) {
                                continue;
                            }
                            if (v2_2.getValue().isEmpty()) {
                                continue;
                            }
                            Iterator<String> v11 = v2_2.getValue().iterator();
                            int v2_3 = 0;
                            while (v11.hasNext()) {
                                v1_2 = v11.next();
                                if (v2_3 > 0) {
                                    v10.append("; ");
                                }
                                v10.append(((String) v1_2));
                                ++v2_3;
                            }
                            v8.addHeader(v3_1, v10.toString());
                            continue;
                        } else {
                            v1_4 = v8.build();
                            break;
                        }
                    } while (true);
                } else {
                    v1_4 = v4;

                }
            } else {
                v1_4 = v4;
            }
        }
        v4 = v1_4;
        Response v2_4 = arg13.proceed(v4);
        if (v7 != null) {
            try {
                v3_2 = new HashMap();
                Headers v4_1 = v2_4.headers;
                if (v4_1 != null) {
                    v1_1 = v4_1.names();
                    if (v1_1 != null) {
                        v5 = v1_1.iterator();
                        while (v5.hasNext()) {
                            v1_2 = v5.next();
                            ((Map) v3_2).put(v1_2, v4_1.values(((String) v1_2)));
                        }
                    }
                }
                v7.put(v6, v3_2);
            } catch (IOException v1_5) {
                Platform.get().log(5, "Saving cookies failed for " + v6.resolve("/..."), ((Throwable) v1_5));
            }
        }

        return v2_4;
    }
}

